//
//  KQButton.m
//  Gxj
//
//  Created by 王奎卿 on 2017/9/12.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "KQButton.h"

@implementation KQButton

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self != nil) {
        
        // 设置文本居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        // 设置图片的内容模式
        self.imageView.contentMode = UIViewContentModeCenter;
    }
    
    return self;
}

/*
 重新titleRectForContentRect: 和 imageRectForContentRect:
 这两个方法修改文本内容位置和图片内容位置
 */

// 修改文本内容位置
//- (CGRect)titleRectForContentRect:(CGRect)contentRect{
//    
//    NSLog(@"%s",__func__);
//    
//    return CGRectMake(0, 0, 100, 70);
//}

// 修改图片内容位置
//- (CGRect)imageRectForContentRect:(CGRect)contentRect{
//    
//    NSLog(@"%s",__func__);
//    self.imageEdgeInsets = UIEdgeInsetsMake(2, 2, self.frame.size.width - 6, self.frame.size.height - 6);
//    CGRect rect;
//    //根据edgeInsets，修改图片的bounds
////    rect.origin.x -= self.imageEdgeInsets.left;
////    rect.origin.y -= self.imageEdgeInsets.top;
////    rect.size.width += self.imageEdgeInsets.left + self.imageEdgeInsets.right;
////    rect.size.height += self.imageEdgeInsets.top + self.imageEdgeInsets.bottom;
//    
//    rect.origin.x = self.imageEdgeInsets.left;
//    rect.origin.y = self.imageEdgeInsets.top;
//    rect.size.width = self.imageEdgeInsets.left + self.imageEdgeInsets.right;
//    rect.size.height = self.imageEdgeInsets.top + self.imageEdgeInsets.bottom;
//    return rect;
//}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    NSLog(@"%s",__func__);
    
    // 从新布局文本内容titleLabel
//    self.titleLabel.frame = CGRectMake(70, 0, 100, 70);
    
    // 从新布局图片内容imageView
    
    self.imageEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
    
}

- (void)drawTextInRect:(CGRect)rect
{
    //令绘制区域为原始区域，增加的内边距区域不绘制
    [super drawRect:UIEdgeInsetsInsetRect(rect, self.imageEdgeInsets)];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

//- (UIImage *)image
//{
//    if (!_image)
//    {
//        _image = [[UIImage alloc] init];
//    }
//    return _image;
//}

@end
